home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / ADVANCED / motionblur.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  5.9 KB  |  242 lines

  1.  
  2. /* motionblur.c - by Tom McReynolds, SGI */
  3.  
  4. /* Using the accumulation buffer for motion blur. */
  5.  
  6. #include <GL/glut.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9.  
  10. const GLdouble FRUSTDIM = 100.f;
  11. const GLdouble FRUSTNEAR = 320.f;
  12. const GLdouble FRUSTFAR = 660.f;
  13.  
  14. /*
  15. ** Create a single component texture map
  16. */
  17. GLfloat *make_texture(int maxs, int maxt)
  18. {
  19.     int s, t;
  20.     static GLfloat *texture;
  21.  
  22.     texture = (GLfloat *)malloc(maxs * maxt * sizeof(GLfloat));
  23.     for(t = 0; t < maxt; t++) {
  24.         for(s = 0; s < maxs; s++) {
  25.             texture[s + maxs * t] = ((s >> 4) & 0x1) ^ ((t >> 4) & 0x1);
  26.         }
  27.     }
  28.     return texture;
  29. }
  30.  
  31. enum {SPHERE = 1, CONE};
  32.  
  33. void
  34. render(GLfloat dx, GLfloat dy, GLfloat dz)
  35. {
  36.     /* material properties for objects in scene */
  37.     static GLfloat wall_mat[] = {1.f, 1.f, 1.f, 1.f};
  38.  
  39.     glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
  40.  
  41.     /*
  42.     ** Note: wall verticies are ordered so they are all front facing
  43.     ** this lets me do back face culling to speed things up.
  44.     */
  45.  
  46.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, wall_mat);
  47.  
  48.     /* floor */
  49.     /* make the floor textured */
  50.     glEnable(GL_TEXTURE_2D);
  51.  
  52.     /*
  53.     ** Since we want to turn texturing on for floor only, we have to
  54.     ** make floor a separate glBegin()/glEnd() sequence. You can't
  55.     ** turn texturing on and off between begin and end calls
  56.     */
  57.     glBegin(GL_QUADS);
  58.     glNormal3f(0.f, 1.f, 0.f);
  59.     glTexCoord2i(0, 0);
  60.     glVertex3f(-100.f, -100.f, -320.f);
  61.     glTexCoord2i(1, 0);
  62.     glVertex3f( 100.f, -100.f, -320.f);
  63.     glTexCoord2i(1, 1);
  64.     glVertex3f( 100.f, -100.f, -640.f);
  65.     glTexCoord2i(0, 1);
  66.     glVertex3f(-100.f, -100.f, -640.f);
  67.     glEnd();
  68.  
  69.     glDisable(GL_TEXTURE_2D);
  70.  
  71.     /* walls */
  72.  
  73.     glBegin(GL_QUADS);
  74.     /* left wall */
  75.     glNormal3f(1.f, 0.f, 0.f);
  76.     glVertex3f(-100.f, -100.f, -320.f);
  77.     glVertex3f(-100.f, -100.f, -640.f);
  78.     glVertex3f(-100.f,  100.f, -640.f);
  79.     glVertex3f(-100.f,  100.f, -320.f);
  80.  
  81.     /* right wall */
  82.     glNormal3f(-1.f, 0.f, 0.f);
  83.     glVertex3f( 100.f, -100.f, -320.f);
  84.     glVertex3f( 100.f,  100.f, -320.f);
  85.     glVertex3f( 100.f,  100.f, -640.f);
  86.     glVertex3f( 100.f, -100.f, -640.f);
  87.  
  88.     /* ceiling */
  89.     glNormal3f(0.f, -1.f, 0.f);
  90.     glVertex3f(-100.f,  100.f, -320.f);
  91.     glVertex3f(-100.f,  100.f, -640.f);
  92.     glVertex3f( 100.f,  100.f, -640.f);
  93.     glVertex3f( 100.f,  100.f, -320.f);
  94.  
  95.     /* back wall */
  96.     glNormal3f(0.f, 0.f, 1.f);
  97.     glVertex3f(-100.f, -100.f, -640.f);
  98.     glVertex3f( 100.f, -100.f, -640.f);
  99.     glVertex3f( 100.f,  100.f, -640.f);
  100.     glVertex3f(-100.f,  100.f, -640.f);
  101.     glEnd();
  102.  
  103.  
  104.     glPushMatrix();
  105.     glTranslatef(-80.f + dx, -60.f + dy, -420.f + dz);
  106.     glCallList(SPHERE);
  107.     glPopMatrix();
  108.  
  109.  
  110.     glPushMatrix();
  111.     glTranslatef(-20.f, -80.f, -600.f);
  112.     glCallList(CONE);
  113.     glPopMatrix();
  114.  
  115.     if(glGetError()) /* to catch programming errors; should never happen */
  116.        printf("Oops! I screwed up my OpenGL calls somewhere\n");
  117.  
  118.     glFlush(); /* high end machines may need this */
  119. }
  120.  
  121. enum {NONE, FIELD};
  122.  
  123. int rendermode = NONE;
  124.  
  125. void
  126. menu(int selection)
  127. {
  128.   rendermode = selection;
  129.   glutPostRedisplay();
  130. }
  131.  
  132. GLdouble focus = 420.;
  133.  
  134. /* Called when window needs to be redrawn */
  135. void redraw(void)
  136. {
  137.     int i;
  138.     int max;
  139.     GLfloat dx, dy, dz;
  140.  
  141.     dx =  .5f;
  142.     dy =  1.f;
  143.     dz = -2.f;
  144.  
  145.     glPushMatrix();
  146.     switch(rendermode) {
  147.     case NONE:
  148.       render(0.f, 0.f, 0.f);
  149.       break;
  150.     case FIELD:
  151.       max = 16;
  152.  
  153.       glClear(GL_ACCUM_BUFFER_BIT);
  154.  
  155.       for(i = 0; i < max; i++) {
  156.         render(dx * i, dy * i, dz * i);
  157.         glAccum(GL_ACCUM, 1.f/max);
  158.       } 
  159.       glAccum(GL_RETURN, 1.f);
  160.     break;
  161.     }
  162.  
  163.     glPopMatrix();
  164.     glutSwapBuffers();
  165. }
  166.  
  167. /* ARGSUSED1 */
  168. void key(unsigned char key, int x, int y)
  169. {
  170.     if(key == '\033')
  171.         exit(0);
  172. }
  173.  
  174. const int TEXDIM = 256;
  175. /* Parse arguments, and set up interface between OpenGL and window system */
  176. int
  177. main(int argc, char *argv[])
  178. {
  179.     GLfloat *tex;
  180.     static GLfloat lightpos[] = {50.f, 50.f, -320.f, 1.f};
  181.     static GLfloat sphere_mat[] = {1.f, .5f, 0.f, 1.f};
  182.     static GLfloat cone_mat[] = {0.f, .5f, 1.f, 1.f};
  183.     GLUquadricObj *sphere, *cone, *base;
  184.  
  185.     glutInit(&argc, argv);
  186.     glutInitWindowSize(512, 512);
  187.     glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_ACCUM|GLUT_DOUBLE);
  188.     (void)glutCreateWindow("motion blur");
  189.     glutDisplayFunc(redraw);
  190.     glutKeyboardFunc(key);
  191.  
  192.     glutCreateMenu(menu);
  193.     glutAddMenuEntry("Normal", NONE);
  194.     glutAddMenuEntry("Motion Blur", FIELD);
  195.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  196.  
  197.     /* draw a perspective scene */
  198.     glMatrixMode(GL_PROJECTION);
  199.     glFrustum(-FRUSTDIM, FRUSTDIM, -FRUSTDIM, FRUSTDIM, FRUSTNEAR, FRUSTFAR); 
  200.     glMatrixMode(GL_MODELVIEW);
  201.  
  202.     /* turn on features */
  203.     glEnable(GL_DEPTH_TEST);
  204.     glEnable(GL_LIGHTING);
  205.     glEnable(GL_LIGHT0);
  206.  
  207.     /* place light 0 in the right place */
  208.     glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
  209.  
  210.     /* remove back faces to speed things up */
  211.     glCullFace(GL_BACK);
  212.  
  213.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  214.  
  215.     glNewList(SPHERE, GL_COMPILE);
  216.     /* make display lists for sphere and cone; for efficiency */
  217.     sphere = gluNewQuadric();
  218.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, sphere_mat);
  219.     gluSphere(sphere, 20.f, 20, 20);
  220.     gluDeleteQuadric(sphere);
  221.     glEndList();
  222.  
  223.     glNewList(CONE, GL_COMPILE);
  224.     cone = gluNewQuadric();
  225.     base = gluNewQuadric();
  226.     glRotatef(-90.f, 1.f, 0.f, 0.f);
  227.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cone_mat);
  228.     gluDisk(base, 0., 20., 20, 1);
  229.     gluCylinder(cone, 20., 0., 60., 20, 20);
  230.     gluDeleteQuadric(cone);
  231.     gluDeleteQuadric(base);
  232.     glEndList();
  233.  
  234.     /* load pattern for current 2d texture */
  235.     tex = make_texture(TEXDIM, TEXDIM);
  236.     glTexImage2D(GL_TEXTURE_2D, 0, 1, TEXDIM, TEXDIM, 0, GL_RED, GL_FLOAT, tex);
  237.     free(tex);
  238.  
  239.     glutMainLoop();
  240.     return 0;             /* ANSI C requires main to return int. */
  241. }
  242.